//
//  CHMenuView.m
//  CarFile
//
//  Created by 陈浩 on 2017/4/12.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHMenuView.h"
@interface CHMenuView ()

@property (nonatomic ,strong) NSArray *titles;
@property (nonatomic ,strong) UIView *indicatorView;
@property (nonatomic ,strong) UIButton *selectedBtn;


@end
@implementation CHMenuView

-(instancetype)initWithFrame:(CGRect)frame withTitles:(NSArray *)titles{
    
    if (self = [super initWithFrame:frame]) {
        _titles = titles;
        [self buildUI];
        
    }
    return self;
    
}

-(void)buildUI{
    
    
    self.indicatorView = [[UIView alloc] init];
    self.indicatorView.backgroundColor = [UIColor colorWithHexString:@"168bce" alpha:1.0f];
    CGFloat btnW = UIScreenW / _titles.count;
    
    for (int i = 0; i < _titles.count; i++) {
        
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(i* btnW, 0, btnW, 40)];
        btn.tag = i+100;
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        [btn setTitleColor:[UIColor colorWithHexString:@"333333" alpha:1.0f] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor colorWithHexString:@"168bce" alpha:1.0f] forState:UIControlStateDisabled];
        [btn setTitle:_titles[i] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(titleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(btnW *(i +1), 11, 1, 18)];
        lineView.backgroundColor = [UIColor colorWithHexString:@"999999" alpha:1.0f];
        [self addSubview:lineView];
        
        if (i== 0) {
            btn.enabled = NO;
            self.selectedBtn = btn;
            self.indicatorView.frame = CGRectMake(0, 37, btnW, 3);
        }
        
    }
    [self addSubview:self.indicatorView];
    
    
}

-(void)setIsUnderline:(BOOL)isUnderline{
    _isUnderline = isUnderline;
    
    self.indicatorView.hidden = !isUnderline;
    
}


-(void)setBgColor:(UIColor *)bgColor{
    
    _bgColor = bgColor;
    self.backgroundColor = bgColor;
}

-(void)titleBtnClick:(UIButton *)btn{
    self.selectedBtn.enabled = YES;
    btn.enabled = NO;
    self.selectedBtn  = btn;
    
    [UIView animateWithDuration:0.2 animations:^{
        self.indicatorView.frame = CGRectMake(btn.x, 37, btn.w, 3);
        
    }];
    if ([self.delegate respondsToSelector:@selector(CHMenuView:didBtnClickWithTag:)]) {
        [self.delegate CHMenuView:self didBtnClickWithTag:btn.tag];
    }
    
    
}


@end
